#pragma  once

class CWriterStream : public CReaderStream
{
public:
	CWriterStream()
	{

	}
	~CWriterStream()
	{
		clear();
	}
public:
	

	template<class T> CWriterStream& operator << (const T& t)
	{
		if (_wpos + (int)sizeof(T) > _capacity)
		{
			setCapacity(_wpos + sizeof(T));
		}
		memcpy(_buffer + _wpos, &t, sizeof(T));
		_wpos += sizeof(T);
		return *this;
	}

	CWriterStream& operator << (const std::string& t)
	{
		SIZE_ST size = (SIZE_ST)t.length();
		if (_wpos + sizeof(SIZE_ST) + size + sizeof(char) > _capacity)
		{
			setCapacity(_wpos + sizeof(SIZE_ST) + size + sizeof(char));
		}

		memcpy(_buffer + _wpos, &size,sizeof(SIZE_ST) );
		_wpos += sizeof(SIZE_ST);
		memcpy(_buffer + _wpos, t.c_str(), size);
		_wpos += size;
		memcpy(_buffer + _wpos, (char*)0, sizeof(char));
		_wpos += sizeof(char);
	}

	CWriterStream& operator << (char* t)
	{
		SIZE_ST size = (SIZE_ST)strlen(t);
		if (_wpos + sizeof(SIZE_ST) + size + sizeof(char) > _capacity)
		{
			setCapacity(_wpos + sizeof(SIZE_ST) + size + sizeof(char));
		}
		memcpy(_buffer + _wpos, &size, sizeof(SIZE_ST));
		_wpos += sizeof(SIZE_ST);
		memcpy(_buffer + _wpos, t, size);
		_wpos += size;
		static const char tmp = 0;
		memcpy(_buffer + _wpos, &tmp, sizeof(char));
		_wpos += sizeof(char);
		return *this;
	}

	CWriterStream& operator << (const char* t)
	{
		SIZE_ST size = (SIZE_ST)strlen(t);
		if (_wpos + sizeof(SIZE_ST) + size + sizeof(char) > _capacity)
		{
			setCapacity(_wpos + sizeof(SIZE_ST) + size + sizeof(char));
		}
		memcpy(_buffer + _wpos, &size, sizeof(SIZE_ST));
		_wpos += sizeof(SIZE_ST);
		memcpy(_buffer + _wpos, t, size);
		_wpos += size;
		static const char tmp = 0;
		memcpy(_buffer + _wpos, &tmp, sizeof(char));
		_wpos += sizeof(char);
		return *this;
	}
};